package cc.cc4414.cc03.sys.service.impl;

import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.cc4414.cc03.core.constant.RedisConsts;
import cc.cc4414.cc03.core.constant.RegexpConsts;
import cc.cc4414.cc03.sys.entity.User;
import cc.cc4414.cc03.sys.mapper.UserMapper;
import cc.cc4414.cc03.sys.service.IUserService;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2018-12-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private TokenStore tokenStore;

	@Override
	public User getUser() {
		User user = new User();
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		BeanUtils.copyProperties(userDetails, user);
		return user;
	}

	@Override
	public User getOneByUsername(String username) {
		String column = "";
		// 判断是用户名还是手机号
		if (Pattern.matches(RegexpConsts.USERNAME, username)) {
			column = User.USERNAME;
		} else if (Pattern.matches(RegexpConsts.MOBILE, username)) {
			column = User.MOBILE;
		} else {
			return null;
		}
		// 根据条件查询用户
		Wrapper<User> queryWrapper = new QueryWrapper<User>().eq(column, username).eq(User.STATE, 0);
		return getOne(queryWrapper);
	}

	@Override
	public Integer getLoginNumber(String username) {
		String number = stringRedisTemplate.opsForValue().get(RedisConsts.LOGIN_NUMBER + username);
		return Integer.parseInt(number);
	}

	@Override
	public void createCaptcha(String visitorId, OutputStream out) {
		ICaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100);
		// 将验证码存入redis，并设置5分钟过期
		stringRedisTemplate.opsForValue().set(RedisConsts.CAPTCHA + visitorId, captcha.getCode(), 5L, TimeUnit.MINUTES);
		captcha.write(out);
	}

	@Override
	public void removeToken(Authentication authentication) {
		OAuth2AccessToken accessToken = tokenStore.getAccessToken((OAuth2Authentication) authentication);
		OAuth2RefreshToken refreshToken = accessToken.getRefreshToken();
		tokenStore.removeAccessTokenUsingRefreshToken(refreshToken);
		tokenStore.removeRefreshToken(refreshToken);
	}

}
